package com.jgssp.ad.sdk.mediation.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.mediation.MediationConstant;
import com.bytedance.sdk.openadsdk.mediation.bridge.custom.reward.MediationCustomRewardVideoLoader;
import com.bytedance.sdk.openadsdk.mediation.custom.MediationCustomServiceConfig;
import com.bytedance.sdk.openadsdk.mediation.custom.MediationRewardItem;
import com.jgssp.ad.sdk.mediation.utils.PriceUtil;
import com.jgssp.ad.sdk.mediation.utils.ThreadUtils;

import java.util.Map;

import cn.jiguang.jgssp.ad.ADJgRewardVodAd;
import cn.jiguang.jgssp.ad.data.ADJgRewardVodAdInfo;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgRewardVodAdListener;

/**
 * @Description: 激励视频适配器
 */
public class CustomerRewardAdapter extends MediationCustomRewardVideoLoader {

    private ADJgRewardVodAd rewardAd;
    private ADJgRewardVodAdInfo rewardAdInfo;

    @Override
    public void load(final Context context, AdSlot adSlot, final MediationCustomServiceConfig serviceConfig) {
        ThreadUtils.runOnUIThreadByThreadPool(new Runnable() {
            @Override
            public void run() {

                if (!(context instanceof Activity)) {
                    callLoadFail(-1, "JgSSP Context must be Activity");
                    return;
                }

                rewardAd = new ADJgRewardVodAd((Activity) context);
                rewardAd.setListener(new ADJgRewardVodAdListener() {

                    @Override
                    public void onAdReceive(ADJgRewardVodAdInfo adInfo) {
                        rewardAdInfo = adInfo;

                        if (isClientBidding() && adInfo != null) {//bidding广告类型
                            //bidding广告成功回调，回传竞价广告价格 单位：分
                            callLoadSuccess(PriceUtil.toCent(adInfo.getECPM()));
                        } else {
                            //普通广告成功回调
                            callLoadSuccess();
                        }
                    }

                    @Override
                    public void onReward(ADJgRewardVodAdInfo adInfo) {
                        callRewardVideoRewardVerify(new MediationRewardItem() {
                            @Override
                            public boolean rewardVerify() {
                                return true;
                            }

                            @Override
                            public float getAmount() {
                                return 0;
                            }

                            @Override
                            public String getRewardName() {
                                return null;
                            }

                            @Override
                            public Map<String, Object> getCustomData() {
                                return null;
                            }
                        });
                    }

                    @Override
                    public void onAdExpose(ADJgRewardVodAdInfo adInfo) {
                        callRewardVideoAdShow();
                    }

                    @Override
                    public void onAdClick(ADJgRewardVodAdInfo adInfo) {
                        callRewardVideoAdClick();
                    }

                    @Override
                    public void onAdClose(ADJgRewardVodAdInfo adInfo) {
                        callRewardVideoAdClosed();
                    }

                    @Override
                    public void onVideoComplete(ADJgRewardVodAdInfo adInfo) {
                        callRewardVideoComplete();
                    }

                    @Override
                    public void onVideoCache(ADJgRewardVodAdInfo adJgRewardVodAdInfo) {

                    }

                    @Override
                    public void onVideoError(ADJgRewardVodAdInfo adJgRewardVodAdInfo, ADJgError adJgError) {
                        callRewardVideoError();
                    }

                    @Override
                    public void onAdFailed(ADJgError error) {
                        callLoadFail(error.getCode(), error.getError());
                    }

                });

                if (serviceConfig != null) {
                    rewardAd.loadAd(serviceConfig.getADNNetworkSlotId());
                }
            }
        });
    }

    @Override
    public void showAd(final Activity activity) {
        Log.i(Const.TAG, "showAd " + Thread.currentThread().toString());
        if (rewardAdInfo != null && activity != null && !activity.isFinishing()) {
            rewardAdInfo.showRewardVod(activity);
        }
    }

    @Override
    public MediationConstant.AdIsReadyStatus isReadyCondition() {
        if (rewardAdInfo != null && rewardAdInfo.isReady()) {
            return MediationConstant.AdIsReadyStatus.AD_IS_READY;
        } else {
            return MediationConstant.AdIsReadyStatus.AD_IS_NOT_READY;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (rewardAd != null) {
            rewardAd.release();
            rewardAd = null;
        }
        if (rewardAdInfo != null) {
            rewardAdInfo.release();
            rewardAdInfo = null;
        }
    }

    @Override
    public void receiveBidResult(boolean win, double winnerPrice, int loseReason, Map<String, Object> extra) {
    }

    /**
     * 是否clientBidding广告
     *
     * @return
     */
    public boolean isClientBidding() {
        return getBiddingType() == MediationConstant.AD_TYPE_CLIENT_BIDING;
    }

}
